package com.hsxxherp.ation.business.user.center.controller;

import com.hsxxherp.ation.business.user.center.entity.UcUserEntity;
import com.hsxxherp.ation.business.user.center.service.IUcUserService;
import com.hsxxherp.ation.business.user.dto.LoginByEmailRequest;
import com.hsxxherp.ation.common.error.AtionErrorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Baomidou
 * @since 2024-06-16
 */
@RestController
@RequestMapping("/user")
public class UcUserController {

    private static final Logger log = LoggerFactory.getLogger(UcUserController.class);


    public final IUcUserService ucUserService;

    public final UcUserEntity ucUserEntity;


    public UcUserController(IUcUserService ucUserService,UcUserEntity ucUserEntity) {
        this.ucUserService = ucUserService;
        this.ucUserEntity = ucUserEntity;
    }


    /**
     * 添加用户
     *
     * @param params
     * @return
     */
    @PostMapping
    public Map<String, Object> add(@RequestBody Map<String, Object> params) {
        if (!ucUserService.saveUSer(params)) {
            throw new RuntimeException("用户添加失败！");
        }

        ucUserEntity.mapToObject(params);
        return params;
    }

    /**
     * 通过邮箱登录密码
     *
     * @param loginByEmailRequest
     * @return
     */
    @PostMapping("/login")
    public boolean loginByEmail(@RequestBody LoginByEmailRequest loginByEmailRequest) throws AtionErrorException {
        loginByEmailRequest.validator();
        return ucUserService.loginByEmail(loginByEmailRequest);
    }

}
